package jmu.zzh.alumnus.business.controller.admin;

import jmu.zzh.alumnus.common.response.PageResponse;
import jmu.zzh.alumnus.common.response.R;
import jmu.zzh.alumnus.business.request.NewsListRequest;
import jmu.zzh.alumnus.business.request.NewsRequest;
import jmu.zzh.alumnus.business.response.NewsResponse;
import jmu.zzh.alumnus.business.service.NewsService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/news")
public class NewsAdminController {
    @Autowired
    NewsService newsService;

    @GetMapping("list")
    public R<PageResponse<NewsResponse>> list(@Valid NewsListRequest request) {
        PageResponse<NewsResponse> list = newsService.list(request);
        return R.ok(list);
    }

    @GetMapping("{id}")
    public R<NewsResponse> getById(@PathVariable Long id) {
        NewsResponse news = newsService.getById(id);
        return R.ok(news);
    }

    @PostMapping
    public R<Boolean> add(@Valid @RequestBody NewsRequest request) {
        newsService.add(request);
        return R.ok();
    }

    @PutMapping
    public R<Boolean> update(@Valid @RequestBody NewsRequest request) {
        newsService.update(request);
        return R.ok();
    }

    @DeleteMapping("{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        newsService.delete(id);
        return R.ok();
    }
}
